package pl.perseus.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

public abstract class BaseAction {

	protected String name = this.getClass().getSimpleName();
	protected static final String SUCCESS = "success";
	protected static final String ERROR = "error";
	protected static final String NONE = "none";
	protected static final String JSON = "json";
	private final SimpleDateFormat format = new SimpleDateFormat();
	
	public abstract String execute();
	
	protected void logAction(){
		this.logAction("");
	}
	
	protected void logAction(String message){
		Logger.getLogger(this.getClass()).log(Level.INFO, name +": "+  message + " " + format.format(Calendar.getInstance().getTime()) + " Context: " + getCurrentUserLogin());
	}
	
	public SecurityContext getContext(){
		return SecurityContextHolder.getContext();
	}
	
	protected String getCurrentUserLogin(){
		return ((org.springframework.security.core.userdetails.User)
				SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
	}
	
}
